/*
 * OrderMessageDrivenBean.java
 *
 * Created on 2 June 2007, 13:55
 * Copyright (C) 2007   Yuezhong Zhang
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

package usyd.comp5348;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Resource;
import javax.ejb.ActivationConfigProperty;
import javax.ejb.EJB;
import javax.ejb.MessageDriven;
import javax.ejb.MessageDrivenContext;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;
import javax.jms.TextMessage;

/**
 * Entity class OrderMessageDrivenBean
 *
 * @author Yuezhong Zhang
 */
@MessageDriven(mappedName = "jms/OrderMessageDrivenBean", activationConfig =  {
    @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge"),
    @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue")
})

public class OrderMessageDrivenBean implements MessageListener {
    
    @EJB
    private OrderFacadeLocal orderFacadeBean;
    @Resource
    private MessageDrivenContext mdc;
    /** Creates a new instance of OrderMessageDrivenBean */
    public OrderMessageDrivenBean() {
    }
    
    public void onMessage(Message message) {
        try {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE,"received update order status message from warehouse");
            ObjectMessage msg = null;
            if (message instanceof TextMessage) {
                msg = (ObjectMessage) message;
                Object obj = msg.getObject();
                if(obj instanceof String){
                    String txt = (String) obj;
                    if(txt.startsWith("UPDATE_ORDER_ID")) {
                        int orderId = Integer.parseInt(txt.substring(txt.indexOf("=") +1, txt.length()));
                        orderFacadeBean.updateOrderStatus(orderId, Order.DELIVERED);
                        Logger.getLogger(getClass().getName()).log(Level.SEVERE,"updateed order status");
                    }else{
                        Logger.getLogger(getClass().getName()).log(Level.SEVERE, txt);
                        System.out.println("failed to delivery order");
                    }
                }
            }
        } catch (JMSException ex) {
            ex.printStackTrace();
            mdc.setRollbackOnly();
        } catch (OrderException ex) {
            ex.printStackTrace();
        }
        
    }
}
